﻿using FySystem.Extension;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace FySystem.IO
{
    public class FileIO
    {
        /// <summary>
        /// 删除过期文件
        /// </summary>
        /// <param name="expiredDays"></param>
        public static void DeleteExpiredFiles(string path, int expiredDays)
        {
            try
            {
                DirectoryInfo di = new DirectoryInfo(path);
                if (di.Exists == false)
                    return;

                FileInfo[] fis = di.GetFiles();
                if (fis.IsNullOrEmpty() == false)
                {
                    DateTime now = DateTime.Now;
                    foreach(var fi in fis)
                    {
                        if ((now - fi.CreationTime).TotalDays >= expiredDays)
                            fi.Delete();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 保存base64字符串的文件
        /// </summary>
        /// <param name="fullname"></param>
        /// <param name="file64"></param>
        /// <exception cref="NotImplementedException"></exception>
        public static void SaveBase64File(string fullname, string file64)
        {
            try
            {
                byte[] data = ImgIO.Base64ImgToBytes(file64);

                FileInfo fi = new FileInfo(fullname);
                var parent = fi.Directory;
                if (parent.Exists == false)
                    parent.Create();

                using(var fs = fi.OpenWrite())
                {
                    fs.Write(data);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 根据操作系统格式化目录字符串
        /// </summary>
        /// <param name="path"></param>
        /// <param name="endsWithSp">是否要在最后加上分隔符</param>
        /// <returns></returns>
        public static string FormatPath(string path, bool endsWithSp = true)
        {
            string res = string.Empty;
            try
            {
                if (string.IsNullOrWhiteSpace(path))
                    return res;

                char sp = '\\';
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    res = path.Replace('/', '\\');
                    sp = '\\';
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
                {
                    res = path.Replace('\\', '/');
                    sp = '/';
                }

                if (res.EndsWith(sp) == false && endsWithSp)
                    res += sp;
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="shortName"></param>
        public static void DeleteFile(string path, string shortName)
        {
            try
            {
                path = FileIO.FormatPath(path, true);
                DeleteFile(path + shortName);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="fullName"></param>
        public static void DeleteFile(string fullName)
        {
            try
            {
                if (File.Exists(fullName))
                    File.Delete(fullName);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
